//
//  SexPickerView.swift
//  sss
//
//  Created by hrrMac on 2020/6/2.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import UIKit

class SexPickerView: MultiPickerView {
    
    ///地址选择器
    var pickerView:UIPickerView = {
        let pickerView = UIPickerView(frame: CGRect(x: 0, y: M_S_TOP_HEIGHT + 0.5, width: M_S_W, height: M_S_PICKER_HEIGHT))
        pickerView.backgroundColor = UIColor.white
        return pickerView
    }()
    
    ///数据源
    var sourceArr = ["男","女"]
    ///选择数据之后的回调
    var sureBlock:((_ index: Int , _ value: String)->Void)?
        
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configPickerView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configPickerView(){
        alertView.addSubview(pickerView)
        pickerView.delegate = self
    }
    
    @objc internal override func sureBtnClick(){
        super.sureBtnClick()
        guard let block = sureBlock else { return }
        block(defaultIndex, sourceArr[defaultIndex])
    }
       
}


///API
extension SexPickerView{
    ///性别选择
    static func showSexPicker(_ defalutIndex: Int = 0) -> SexPickerView{
        let pickerView = SexPickerView()
        pickerView.show()
        pickerView.defaultIndex = defalutIndex
        pickerView.reload()
        return pickerView
    }
    
    ///单列自定义选择器
    static func showCustomerPicker(_ dataArr: Array<String>, defalutIndex: Int = 0) -> SexPickerView{
        let pickerView = SexPickerView()
        pickerView.sourceArr = dataArr
        pickerView.show()
        pickerView.defaultIndex = defalutIndex
        pickerView.reload()
        return pickerView
    }
    
    
    func reload(){
        pickerView.reloadAllComponents()
        if sourceArr.count == 0 {
            return
        }
        if defaultIndex > sourceArr.count {
            defaultIndex = 0
        }
        pickerView.selectRow(self.defaultIndex, inComponent: 0, animated: false)
    }
    
    
   
}



extension SexPickerView:UIPickerViewDelegate,UIPickerViewDataSource{
    //有多少列
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return sourceArr.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return sourceArr[row]
    }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        defaultIndex = row
    }
    
}



